package org.reliablesource.jdt.viewers;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.viewers.IOpenListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PartInitException;
import org.reliablesource.client.ui.viewers.ReliableViewer;
import org.reliablesource.jdt.model.IContributor;
import org.reliablesource.jdt.model.ISourceElementInfo;
import org.reliablesource.jdt.model.JavaElementAdapter;
import org.reliablesource.model.core.IReliableElement;

/**
 *
 *  ContributorViewer.java<br>
 *  Copyright (c) 2007, Reliable Source, Inc. All Rights Reserved<br><br>
 *
 *	Created on: May 11, 2007 <br><br>
 *  @author Dennis Park 
 *<a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *
 * @since 1.0.0
 */
public class ContributorViewer extends ReliableViewer implements
		ISelectionChangedListener {

	protected Map<String, Label> controlMap;

	public static final int TOTAL = 0;
	public static final int LOC = 1;

	public static final String[] PROPS = { Messages.ContributorViewer_0, Messages.ContributorViewer_1 };
	public static final int[] INDEX = { TOTAL, LOC };

	/* (non-Javadoc)
	 * @see org.reliablesource.client.ui.viewers.ReliableViewer#init()
	 */
	@Override
	public void init() {
		super.init();
		controlMap = new HashMap<String, Label>();
	}

	@Override
	public void createViewerControl(final Composite _folder) {
		contentComposite = new Composite(_folder, SWT.NULL);
		GridLayout _layout = new GridLayout();
		_layout.numColumns = 2;
		contentComposite.setLayout(_layout);

		GridData _gd = new GridData(SWT.FILL, SWT.FILL, true, false);
		_gd = new GridData(SWT.FILL, SWT.FILL, true, false);

		Group _lhs = new Group(contentComposite, SWT.NULL);
		_lhs.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		_layout = new GridLayout();
		_layout.numColumns = 1;
		_lhs.setLayout(_layout);
		_lhs.setText(Messages.ContributorViewer_2);

		Group _group = new Group(contentComposite, SWT.NULL);
		_gd = new GridData(SWT.FILL, SWT.FILL, true, true);
		_group.setLayoutData(_gd);
		_group.setText(Messages.ContributorViewer_3);

		_layout = new GridLayout();
		_group.setLayout(_layout);
		_layout.numColumns = 2;
		_group.setLayout(_layout);

		_gd = new GridData(SWT.FILL, SWT.FILL, true, true);
		contentViewer = new TreeViewer(_lhs, SWT.BORDER | SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.MULTI);
		contentViewer.getControl().setLayoutData(_gd);
		((TreeViewer)contentViewer).addOpenListener(new IOpenListener(){
			public void open(OpenEvent event) {
				IStructuredSelection _sel = (IStructuredSelection) event.getSelection();
				if(!(_sel.getFirstElement() instanceof ISourceElementInfo))
					return;
				try {
					JavaUI.openInEditor(((ISourceElementInfo)_sel.getFirstElement()).getJavaElement());
				} catch (PartInitException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (JavaModelException e) {
				}
			}
		});

		int _nControls = PROPS.length;
		for (int i = 0; i < _nControls; i++) {
			Label _lbl = new Label(_group, SWT.NONE);
			_lbl.setText(PROPS[i] + " : "); //$NON-NLS-1$
			_gd = new GridData(SWT.FILL, SWT.NULL, false, false);
			_lbl.setLayoutData(_gd);

			_gd = new GridData(SWT.FILL, SWT.NULL, true, false);
			_lbl = new Label(_group, SWT.BORDER);
			_lbl.setLayoutData(_gd);
			controlMap.put(PROPS[i], _lbl);
		}

		contentViewer.setContentProvider(new ContributorContentProvider());
		contentViewer.setLabelProvider(new ContributorLabelProvider());
		addChild(contentViewer);
		contentViewer.addSelectionChangedListener(this);
	}

	/* (non-Javadoc)
	 * @see org.reliablesource.client.ui.viewers.ReliableViewer#setInput(java.lang.Object)
	 */
	@Override
	public void setInput(final Object _input) {
		contentViewer.setInput(_input);
	}

	public void selectionChanged(final SelectionChangedEvent event) {
		StructuredSelection _selection = (StructuredSelection) event
				.getSelection();
		if (_selection.isEmpty())
			return;
		IReliableElement[] _selections = (IReliableElement[]) _selection
				.toList().toArray(new IReliableElement[_selection.size()]);
		int _nSelections = _selections.length;
		int _loc = 0;
		int _files = 0;
		for (int i = 0; i < _nSelections; i++) {
				_loc += calculateTotal(_selections[i], LOC);
				_files += calculateTotal(_selections[i], TOTAL);
		}

		controlMap.get(PROPS[LOC]).setText(Integer.toString(_loc));
		controlMap.get(PROPS[TOTAL]).setText(Integer.toString(_files));
	}
	
	
	private int calculateTotal(final IReliableElement _selection, final int property){
		
		int _returnValue = 0;
		switch(property){
		case LOC:
			if (_selection instanceof IContributor) {
				_returnValue  =  ((IContributor) _selection).getLOC();
			} else if (_selection instanceof ISourceElementInfo) {
				_returnValue =Integer.parseInt(((ISourceElementInfo) _selection).getNumLines());
			}else if (_selection instanceof JavaElementAdapter) {
				_returnValue =((JavaElementAdapter) _selection).getNumLines();				
			}
			break;
		case TOTAL:
			if (_selection instanceof IContributor) {
				_returnValue  =  ((IContributor) _selection).getNumFiles();
//			} 
//			else if (_selection instanceof ISourceElementInfo) {
//				_returnValue = 1;
				
			}else if (_selection instanceof JavaElementAdapter) {
				_returnValue =((JavaElementAdapter) _selection).getNumFiles();				
			}
			break;
		}
		return _returnValue;
	}
}
